﻿Imports System.Data.Entity
Imports HSPelections.Models

Public Class VotesController
    Inherits System.Web.Mvc.Controller

    Private db As New VotesContext

    '
    ' GET: /Votes/

    Function Index() As ActionResult
        Return View(db.Votes.ToList())
    End Function

    '
    ' GET: /Votes/Details/5

    Function Details(Optional ByVal id As Integer = Nothing) As ActionResult
        Dim vote As Vote = db.Votes.Find(id)
        If IsNothing(vote) Then
            Return HttpNotFound()
        End If
        Return View(vote)
    End Function

    '
    ' GET: /Votes/Create

    Function Create() As ActionResult
        Return View()
    End Function

    '
    ' POST: /Votes/Create

    <HttpPost()> _
    <ValidateAntiForgeryToken()> _
    Function Create(ByVal vote As Vote) As ActionResult

        If ModelState.IsValid Then
            db.Votes.Add(vote)
            db.SaveChanges()
            Return RedirectToAction("Index")
        End If

        Return View(vote)
    End Function

    '
    ' GET: /Votes/Edit/5

    Function Edit(Optional ByVal id As Integer = Nothing) As ActionResult
        Dim vote As Vote = db.Votes.Find(id)
        If IsNothing(vote) Then
            Return HttpNotFound()
        End If
        Return View(vote)
    End Function

    '
    ' POST: /Votes/Edit/5

    <HttpPost()> _
    <ValidateAntiForgeryToken()> _
    Function Edit(ByVal vote As Vote) As ActionResult
        If ModelState.IsValid Then
            db.Entry(vote).State = EntityState.Modified
            db.SaveChanges()
            Return RedirectToAction("Index")
        End If

        Return View(vote)
    End Function

    '
    ' GET: /Votes/Delete/5

    Function Delete(Optional ByVal id As Integer = Nothing) As ActionResult
        Dim vote As Vote = db.Votes.Find(id)
        If IsNothing(vote) Then
            Return HttpNotFound()
        End If
        Return View(vote)
    End Function

    '
    ' POST: /Votes/Delete/5

    <HttpPost()> _
    <ActionName("Delete")> _
    <ValidateAntiForgeryToken()> _
    Function DeleteConfirmed(ByVal id As Integer) As RedirectToRouteResult
        Dim vote As Vote = db.Votes.Find(id)
        db.Votes.Remove(vote)
        db.SaveChanges()
        Return RedirectToAction("Index")
    End Function

    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        db.Dispose()
        MyBase.Dispose(disposing)
    End Sub

    Function MakeVote1() As ActionResult
        Dim newVote As New MakeVote
        Dim newballet As List(Of String) = newVote.createVote()
        Dim voteObject As New Vote
        Dim number As Integer = db.Votes.Count()
        voteObject.VoteId = number
        voteObject.President = newballet.Item(0)
        voteObject.VicePresident = newballet.Item(1)
        voteObject.Secretary = newballet.Item(2)
        voteObject.Treasurer = newballet.Item(3)
        voteObject.SergeantAtArms = newballet.Item(4)
        db.Votes.Add(voteObject)
        db.SaveChanges()
        Return RedirectToAction("Index")
    End Function
    Function FindPercents()
        Return 0
    End Function
End Class